package com.juhuacha.tryrxjava.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.juhuacha.tryrxjava.R;

/**
 * Created by Administrator on 2016/8/29.
 * 邮箱       juhuacha18@gmail.com
 * E-Mail     juhuacha18@gmail.com
 * CopyRight  9hgame.com reserved
 */
public class CustomBar extends RelativeLayout {

    private final int mLeftTextColor;
    private final Drawable mLeftBackground;
    private final String mLeftText;
    private final int mRightTextColor;
    private final Drawable mRightBackground;
    private final String mRightText;
    private final int mTitleTextColor;
    private final float mTitleTextSize;
    private final String mTitleText;
    private Button mLeftButton;
    private Button mRightButton;
    private TextView mTitle;
    private LayoutParams mLeftParams;
    private LayoutParams mRightParams;
    private LayoutParams mTitleParams;
    private topbarClickListener mLisener;

    public CustomBar(Context context) {
        //super(context);
        this(context, null, 0);
    }

    public CustomBar(Context context, AttributeSet attrs) {
        //super(context, attrs);
        this(context, attrs, 0);

    }

    public CustomBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TopBar);

        mLeftTextColor = array.getColor(R.styleable.TopBar_leftTextColor, 0);
        mLeftBackground = array.getDrawable(R.styleable.TopBar_leftBackground);
        mLeftText = array.getString(R.styleable.TopBar_leftText);

        mRightTextColor = array.getColor(R.styleable.TopBar_rightTextColor, 0);
        mRightBackground = array.getDrawable(R.styleable.TopBar_rightBackground);
        mRightText = array.getString(R.styleable.TopBar_rightText);

        mTitleTextColor = array.getColor(R.styleable.TopBar_titleTextColor, 0);
        mTitleTextSize = array.getDimension(R.styleable.TopBar_titleTextSize, 10);
        mTitleText = array.getString(R.styleable.TopBar_title);

        //避免重新创建时候的错误
        array.recycle();

        initTitleView(context);
    }

    /**
     * 初始化头部bar布局
     */
    private void initTitleView(Context context) {

        mLeftButton = new Button(context);
        mRightButton = new Button(context);
        mTitle = new TextView(context);

        mLeftButton.setBackground(mLeftBackground);
        mLeftButton.setText(mLeftText);
        mLeftButton.setTextColor(mLeftTextColor);

        mRightButton.setBackground(mRightBackground);
        mRightButton.setText(mRightText);
        mRightButton.setTextColor(mRightTextColor);

        mTitle.setTextSize(mTitleTextSize);
        mTitle.setText(mTitleText);
        mTitle.setTextColor(mTitleTextColor);
        mTitle.setGravity(Gravity.CENTER);

        mLeftParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mRightParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        mTitleParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);

        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        mTitleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);

        addView(mLeftButton,mLeftParams);
        addView(mRightButton,mRightParams);
        addView(mTitle,mTitleParams);

        mLeftButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mLisener != null){
                    mLisener.leftClick();
                }
            }
        });
        mRightButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                if(mLisener != null){
                    mLisener.rightClick();
                }
            }
        });
    }

    public void setOnTopBarClickListener(topbarClickListener listener){
        this.mLisener = listener;
    }

    public void setButtonVisible(int id,boolean flag){
        if(flag){
            if(id == 0){
                mLeftButton.setVisibility(View.VISIBLE);
            }else{
                mRightButton.setVisibility(View.VISIBLE);
            }
        }else{
            if(id == 0){
                mLeftButton.setVisibility(View.GONE);
            }else{
                mRightButton.setVisibility(View.GONE);
            }
        }
    }

    public interface topbarClickListener{
        //左按钮点击
        void leftClick();
        //右按钮点击
        void rightClick();
    }

}
